package org.moonshiner.model.user;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import javax.persistence.CascadeType;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.MappedSuperclass;
import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator;
import javax.persistence.Table;

import org.hibernate.annotations.Parameter;
import org.hibernate.annotations.Type;

import org.moonshiner.model.enums.UserRole;
import org.moonshiner.model.project.Project;

@Entity(name = "user")
@Table(name = "users", schema = "public")
@MappedSuperclass
public class User implements Serializable {

	private static final long serialVersionUID = -1615496413203926953L;

	@Id
	@SequenceGenerator(name = "INCREMENT_ID", sequenceName = "user_id_seq", allocationSize = 1)
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "INCREMENT_ID")
	private Integer id;

	@Column
	private String name;

	@Column
	private String password;

	@Column
	@Type(type = "org.moonshiner.model.enums.PgEnumImpl", parameters = { @Parameter(name = "enumClassName", value = "org.moonshiner.model.enums.UserRole") })
	private UserRole role;

	@OneToMany(fetch = FetchType.LAZY, cascade = { CascadeType.ALL }, mappedBy = "user")
	private List<Project> projects;

	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public UserRole getRole() {
		return role;
	}

	public void setRole(UserRole role) {
		this.role = role;
	}

	public List<Project> getProjects() {
		return projects;
	}

	public void setProjects(List<Project> project) {
		this.projects = project;
	}

	public void addProject(Project project) {
		if (this.projects == null) {
			this.projects = new ArrayList<Project>();
		}
		this.projects.add(project);
	}

}
